package api

import (
	"shop/entity"
	"shop/service"
	"shop/utils/convertutil"
	"shop/utils/result"
	"shop/utils/sessionutil"

	"github.com/gin-gonic/gin"
)

func GetUserOrderList(ctx *gin.Context) {
	pageNo := convertutil.ParseInt(ctx.DefaultQuery("pageNo", "1"))
	pageSize := convertutil.ParseInt(ctx.DefaultQuery("pageSize", "30"))
	result.RenderResult(ctx, service.GetUserOrderList(sessionutil.GetUserIdFromCtx(ctx), pageNo, pageSize))
}

func GetOrderDetail(ctx *gin.Context) {
	result.RenderResult(ctx, service.GetOrderDetail(ctx.Param("orderNo")))
}

func GetOrderStatus(ctx *gin.Context) {
	result.RenderResult(ctx, service.GetOrderStatus(ctx.Param("orderNo")))
}

func CreateOrder(ctx *gin.Context) {
	params := entity.OrderInfo{}
	params.UserId = sessionutil.GetUserIdFromCtx(ctx)
	params.ShopId = sessionutil.GetShopIdFromCtx(ctx)
	err := ctx.ShouldBindJSON(&params)
	if err != nil {
		result.RenderResult(ctx, result.Fail("参数格式错误: "+err.Error()))
		return
	}

	result.RenderResult(ctx, service.CreateOrder(&params))
}

// ConfirmFinishPay 初步确认支付
func ConfirmFinishPay(ctx *gin.Context) {
	result.RenderResult(ctx, service.ConfirmFinishPay(ctx.Param("orderNo"), sessionutil.GetUserIdFromCtx(ctx)))
}

func CancelOrder(ctx *gin.Context) {
	result.RenderResult(ctx, service.CancelOrder(ctx.Param("orderNo"), sessionutil.GetUserIdFromCtx(ctx)))
}

func ConfirmReceive(ctx *gin.Context) {
	result.RenderResult(ctx, service.ConfirmReceiveAndAddGoodsSoldCount(ctx.Param("orderNo"), sessionutil.GetUserIdFromCtx(ctx)))
}
